/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShowCuentas.java
 *
 * Created on Dec 13, 2011, 10:55:48 AM
 */

package INTERFAZ;
import CONTROLES.ControlCuenta;
import CONTROLES.ControlCliente;
import MODELOS.Cliente;
import MODELOS.Cuenta;
import UTILITARIOS.Herramienta;
import UTILITARIOS.MiModelo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
/**
 *
 * @author VICENTE
 */
public class ShowCuentas extends javax.swing.JDialog {

    /** Creates new form ShowCuentas */
    public ShowCuentas(javax.swing.JFrame parent, boolean modal,Cliente c){
        super(parent, modal);
        setAlwaysOnTop(false);
        this.cliente = c;
        initComponents();
        cambiarPropiedades();
        addEventos(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        activas = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        inactivas = new javax.swing.JTable();
        jLabel10 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setFont(new java.awt.Font("Ebrima", 0, 11));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos Personales Del Cliente", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 13))); // NOI18N

        jLabel2.setText("Nombre Completo :");

        jLabel3.setText("Dirección :");

        jLabel4.setText("Teléfono :");

        jLabel5.setBackground(java.awt.Color.white);
        jLabel5.setText("Nombre Completo");

        jLabel6.setText("C.I.:");

        jLabel7.setText("Numero CI");

        jLabel8.setBackground(java.awt.Color.white);
        jLabel8.setText("Direccion");

        jLabel9.setText("Fono");

        jButton1.setText("Nueva Cuenta");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 296, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DATOS DE LAS CUENTAS", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ACTIVAS", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 10))); // NOI18N

        jScrollPane2.setViewportView(activas);

        jButton3.setText("Ver Cuenta");
        jButton3.setEnabled(false);

        jLabel1.setText("NOTA: Para ver una cuenta Seleccionela en la tabla y luego presione el siguiente boton =>");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(237, 237, 237)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3)
                .addGap(8, 8, 8))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Inactivas", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 10), new java.awt.Color(255, 0, 51))); // NOI18N

        jScrollPane1.setViewportView(inactivas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jLabel10.setText("NOTA: Para ver una cuenta Seleccionela en la tabla y luego presione el siguiente boton =>");

        jButton4.setText("Ver Cuenta");
        jButton4.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(282, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addComponent(jButton4)
                .addGap(12, 12, 12))
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jButton4))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jButton2.setText("Cerrar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    private void cambiarPropiedades(){

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        jLabel5.setText(cliente.getNombreCliente()+" "+cliente.getApellidosCli());
        jLabel7.setText(Integer.toString(cliente.getCiCliente()));
        jLabel8.setText(cliente.getDireccion());
        jLabel9.setText(Integer.toString(cliente.getTelefono()));

        MiModelo modelo_activas = new MiModelo(new Object [][] {},new String [] {"ID","Nº Cuenta", "Plan Inversion","Duración", "Fecha de Apertura", "Monto Inversion $us"}, -1);
        ControlCuenta c_cuenta = new ControlCuenta();
        cuentas_activas = new ArrayList<Cuenta>();
        cuentas_activas = c_cuenta.getCuentas(cliente, "ACTIVA");
        Cuenta cuenta_aux;
        if(cuentas_activas.size()>0){
            for(int i=0;i<cuentas_activas.size();i++){
                cuenta_aux = cuentas_activas.get(i);
                Object [] filaTablaActivas = {
                    cuenta_aux.getIdCuenta(),
                    cuenta_aux.getNumeroCuenta(),
                    cuenta_aux.getPlan().getNombrePlan(),
                    cuenta_aux.getPlan().getDuracion()+" Meses",
                    df.format(cuenta_aux.getFechaApertura()),
                    Herramienta.redondearDoubleString(cuenta_aux.getMonto())};
                    modelo_activas.addRow(filaTablaActivas);
            }
        }
        activas.setModel(modelo_activas);
        activas.getTableHeader().setReorderingAllowed(false) ;//para impedir que se muevan las columnas de la tabla

        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.RIGHT);

        DefaultTableCellRenderer tcrc = new DefaultTableCellRenderer();
        tcrc.setHorizontalAlignment(SwingConstants.CENTER);

        activas.getColumnModel().getColumn(0).setCellRenderer(tcr);
        activas.getColumnModel().getColumn(0).setMaxWidth(80);
        activas.getColumnModel().getColumn(1).setCellRenderer(tcr);
        activas.getColumnModel().getColumn(1).setMaxWidth(150);
        activas.getColumnModel().getColumn(2).setCellRenderer(tcrc);
        activas.getColumnModel().getColumn(3).setCellRenderer(tcrc);
        activas.getColumnModel().getColumn(4).setCellRenderer(tcrc);
        activas.getColumnModel().getColumn(5).setCellRenderer(tcr);

        MiModelo modelo_i = new MiModelo(
            new Object [][] {},new String [] {"ID","N# Cuenta", "Plan", "Fecha de Apertura", "Fecha Cierre","Estado", "Monto $us"}, -1);
        cuentas_inactivas = new ArrayList<Cuenta>();
        cuentas_inactivas = c_cuenta.getCuentas(cliente, "CERRADA");
        cuentas_inactivas.addAll(c_cuenta.getCuentas(cliente, "CANCELADA"));
        if(cuentas_inactivas.size()>0){
            for(int i=0;i<cuentas_inactivas.size();i++){
                cuenta_aux = cuentas_inactivas.get(i);

                Object [] filaTablaInactivas = {
                    cuenta_aux.getIdCuenta(),
                    cuenta_aux.getNumeroCuenta(),
                    cuenta_aux.getPlan().getNombrePlan(),
                    df.format(cuenta_aux.getFechaApertura()),
                    df.format(cuenta_aux.getFechaCierre()),
                    cuenta_aux.getEstado(),
                    Herramienta.redondearDoubleString(cuenta_aux.getMonto())
                };

               modelo_i.addRow(filaTablaInactivas);
            }
        }
        inactivas.setModel(modelo_i);
        inactivas.getTableHeader().setReorderingAllowed(false) ;//para impedir que se muevan las columnas de la tabla

        inactivas.getColumnModel().getColumn(0).setCellRenderer(tcr);
        inactivas.getColumnModel().getColumn(0).setMaxWidth(80);
        inactivas.getColumnModel().getColumn(1).setCellRenderer(tcr);
        inactivas.getColumnModel().getColumn(1).setMaxWidth(150);
        inactivas.getColumnModel().getColumn(2).setCellRenderer(tcrc);
        inactivas.getColumnModel().getColumn(3).setCellRenderer(tcrc);
        inactivas.getColumnModel().getColumn(4).setCellRenderer(tcrc);
        inactivas.getColumnModel().getColumn(5).setCellRenderer(tcrc);
        inactivas.getColumnModel().getColumn(6).setCellRenderer(tcr);
        inactivas.getColumnModel().getColumn(6).setMaxWidth(170);
    }
    private void addEventos(final javax.swing.JFrame parent){
        final javax.swing.JFrame padre = parent;
        activas.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                jButton3.setEnabled(true);
            }
        });
        inactivas.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                jButton4.setEnabled(true);
            }
        });
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                FormNewCuenta newCuenta = new FormNewCuenta(parent, cliente);
                Herramienta.centerOnScreen(newCuenta);
                newCuenta.setVisible(true);
            }
        });
        jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        jButton3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(activas.getSelectedRow()>= 0){
                    dispose();
                    Cuenta cuenta_ver = cuentas_activas.get(activas.getSelectedRow());
                    VistaCuenta ventana_cuenta = new VistaCuenta(padre, true,cuenta_ver);
                    Herramienta.centerOnScreen(ventana_cuenta);
                    ventana_cuenta.setVisible(true);
                }
            }
        });
        jButton4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(inactivas.getSelectedRow()>= 0){
                    dispose();
                    Cuenta cuenta_ver = cuentas_inactivas.get(inactivas.getSelectedRow());
                    VistaCuenta ventana_cuenta = new VistaCuenta(padre, true,cuenta_ver);
                    Herramienta.centerOnScreen(ventana_cuenta);
                    ventana_cuenta.setVisible(true);
                }
            }
        });
    }
    public static void main(String args[]) {
        
        ShowCuentas dialog = new ShowCuentas(new javax.swing.JFrame(), true, new ControlCliente().dameCliente(1));
        dialog.setVisible(true);
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable activas;
    private javax.swing.JTable inactivas;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private Cliente cliente;
    private ArrayList<Cuenta> cuentas_activas;
    private ArrayList<Cuenta> cuentas_inactivas;
}